package com.tony.mindwarning.controller;

import com.tony.mindwarning.pojo.*;
import com.tony.mindwarning.service.OutbroundService;
import com.tony.mindwarning.service.ProductService;
import com.tony.mindwarning.service.TypeService;
import com.tony.mindwarning.utils.MessageResults;
import com.tony.mindwarning.utils.PageUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: OutbroundController
 * @Auther: 皮蛋布丁
 * @Date: 2021/04/11/20:39
 * @Description:
 */
@Controller
@Scope("prototype")
public class OutbroundController {

    @Resource
    ProductService productService;
    @Resource
    TypeService typeService;
    @Resource
    OutbroundService outbroundService;

    /**
    * @Description: Inbround 商品出库
    * @Param: [outbround, session]
    * @return: com.tony.mindwarning.utils.MessageResults
    * @Author: 皮蛋布丁
    */
    @ResponseBody
    @RequestMapping("/outbround_add")
    public MessageResults Inbround(Outbround outbround, HttpSession session){
        MessageResults results = null;
        try {
            User user= (User)session.getAttribute("userLogin");

            Product product = productService.findById(outbround.getPid());
            outbround.setPname(product.getPtitle());
            //判断出库是否溢出
            if (product.getPcount()-outbround.getOutcount()<0) {

                results = new MessageResults(500, "出库失败", null);
                return results;
            }
            Type type = typeService.getTypeById(outbround.getTid());
            outbround.setPtype(type.getTname());

            outbround.setOutuser(user.getUname());
            //反转符号，直接更新
            outbround.setOutcount(outbround.getOutcount()*(-1));

            System.out.println("出库的对象信息是："+outbround);
            int count = outbroundService.outbround(outbround,user.getId());
            if (count > 0) {
                results = new MessageResults(200, "出库成功", null);
            } else {
                results = new MessageResults(500, "出库失败", null);
            }
        } catch (Exception e) {
            results = new MessageResults(500, "出库失败", null);
            e.printStackTrace();
        } finally {
        }
        return results;
    }

    /**
    * @Description: queryPage 分类列表(分页)
    * @Param: [pageIndex, pageSize, model]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    */
    @RequestMapping("/outbround_page")
    public String queryPage(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize, Model model) {

        //总条数
        Integer totalCount = outbroundService.getTotlCount();
        Map<String, Object> map = new HashMap<String, Object>();
        //开始查询的位置
        map.put("startIndex",(pageIndex-1)*pageSize);
        map.put("pageSize",pageSize);
        //每页的数据
        List<Outbround> outbrounds = outbroundService.getOutbroundByPage(map);
        //封装数据
        PageUtils<Outbround> pageUtils = new PageUtils<>(pageIndex,pageSize,totalCount,outbrounds);
        System.out.println("分页工具类：" + pageUtils);
        model.addAttribute("pageUtils",pageUtils);
        System.out.println(pageUtils);
        return "outbround";
    }

    /**
     * @Description: delete 根据id删除入库详情
     * @Param: [id]
     * @return: com.tony.mindwarning.utils.MessageResults
     * @Author: 皮蛋布丁
     */
    @ResponseBody
    @RequestMapping("/outbround_delete")
    public MessageResults delete(HttpSession session,Integer id){
        User currUser= (User)session.getAttribute("userLogin");
        Integer count = outbroundService.deleteOutbroundRecord(id,currUser.getId());
        MessageResults results=null;
        if(count>0){
            results=new MessageResults(200,"删除成功",null);
        }else{
            results=new MessageResults(500,"删除失败",null);
        }
        return results;
    }

}
